﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XBeeLib;
namespace XBeeLibSample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            foreach (XBeeLib.XBeeATCmd dow in Enum.GetValues(typeof(XBeeLib.XBeeATCmd)))
            {
                comboBox1.Items.Add(dow);
            }
            comboBox1.Text = XBeeLib.XBeeATCmd.CH.ToString();

        }

        private void xbeeSerialPortControlPanel1__16BitDataPacketReceived(object sender, XbeeSerialPortControlPanel.RX16BitPacket packet)
        {
            textBox1.AppendText("16BitDataPacketReceived,");
            textBox1.AppendText("RSSI:-" + packet.rssi.ToString() + "-dBm");
            if (packet.addressBroadcastFlag)
            {
                textBox1.AppendText(",AddressBroadcast\r\n");
            }
            if (packet.panBroadcastFlag)
            {
                textBox1.AppendText(",PanBroadcastFlag\r\n");
            }
            textBox1.AppendText("DATA:" + BitConverter.ToString(packet.dataArray) + "\r\n");
        }

        private void xbeeSerialPortControlPanel1__64BitDataPacketReceived(object sender, XbeeSerialPortControlPanel.RX64BitPacket packet)
        {
            textBox1.AppendText("64BitDataPacketReceived,");
            textBox1.AppendText("RSSI:-" + packet.rssi.ToString() + "-dBm");
            if (packet.addressBroadcastFlag)
            {
                textBox1.AppendText(",AddressBroadcast\r\n");
            }
            if (packet.panBroadcastFlag)
            {
                textBox1.AppendText(",PanBroadcastFlag\r\n");
            }
            textBox1.AppendText("DATA:" + BitConverter.ToString(packet.dataArray) + "\r\n");

        }

        private void xbeeSerialPortControlPanel1_ATCommandPacketReceived(object sender, XbeeSerialPortControlPanel.ATCommandResponsePacket packet)
        {
            textBox1.AppendText("ATCommandPacketReceived,FrameID:" + packet.frameID.ToString() + ",");
            switch (packet.status)
            {
                case XbeeSerialPortControlPanel.ATCommandResponsePacketStatus.OK:
                    textBox1.AppendText("OK\r\n");
                    if (packet.parameterArray.Length > 0)
                    {
                        textBox1.AppendText(packet.cmd.ToString());
                        textBox1.AppendText(":0x" + BitConverter.ToString(packet.parameterArray) + "\r\n");
                    }
                    else
                    {
                        textBox1.AppendText(packet.cmd.ToString() + "\r\n");
                    }
                    break;
                case XbeeSerialPortControlPanel.ATCommandResponsePacketStatus.ERROR:
                    textBox1.AppendText("ERROR\r\n");
                    break;
                case XbeeSerialPortControlPanel.ATCommandResponsePacketStatus.InvalidCommand:
                    textBox1.AppendText("InvalidCommand\r\n");
                    break;
                case XbeeSerialPortControlPanel.ATCommandResponsePacketStatus.InvalidParameter:
                    textBox1.AppendText("InvalidParameter\r\n");
                    break;
            }

        }

        private void xbeeSerialPortControlPanel1_Connected(object sender, EventArgs e)
        {
            textBox1.AppendText("COMポートに接続しました。\r\n");

            xbeeSerialPortControlPanel1.sendAtCommand(xbeeSerialPortControlPanel1.CreateATCommandData('C', 'H'));
            xbeeSerialPortControlPanel1.sendAtCommand(xbeeSerialPortControlPanel1.CreateATCommandData('I', 'D'));
            xbeeSerialPortControlPanel1.sendAtCommand(xbeeSerialPortControlPanel1.CreateATCommandData('M', 'Y'));
            xbeeSerialPortControlPanel1.sendAtCommand(xbeeSerialPortControlPanel1.CreateATCommandData('S', 'L'));
            xbeeSerialPortControlPanel1.sendAtCommand(xbeeSerialPortControlPanel1.CreateATCommandData('S', 'H'));

        }

        private void xbeeSerialPortControlPanel1_Disconnected(object sender, EventArgs e)
        {
            textBox1.AppendText("COMポートを切断しました。\r\n");
        }

        private void xbeeSerialPortControlPanel1_modemStatusPacketPacketReceived(object sender, XbeeSerialPortControlPanel.ModemStatusPacket packet)
        {
            textBox1.AppendText("ModemStatusPacketPacketReceived,");
            switch (packet.status)
            {
                case XbeeSerialPortControlPanel.ModemStatus.Associated:
                    textBox1.AppendText("Associated\r\n");
                    break;
                case XbeeSerialPortControlPanel.ModemStatus.CoordinatorRealignment:
                    textBox1.AppendText("CoordinatorRealignment\r\n");
                    break;
                case XbeeSerialPortControlPanel.ModemStatus.CoordinatorStarted:
                    textBox1.AppendText("CoordinatorStarted\r\n");
                    break;
                case XbeeSerialPortControlPanel.ModemStatus.Disassociated:
                    textBox1.AppendText("Disassociated\r\n");
                    break;
                case XbeeSerialPortControlPanel.ModemStatus.HardwareReset:
                    textBox1.AppendText("HardwareReset\r\n");
                    break;
                case XbeeSerialPortControlPanel.ModemStatus.SynchronizationLost:
                    textBox1.AppendText("SynchronizationLost\r\n");
                    break;
                case XbeeSerialPortControlPanel.ModemStatus.WatchdogTimerReset:
                    textBox1.AppendText("WatchdogTimerReset\r\n");
                    break;
            }
        }

        private void xbeeSerialPortControlPanel1_TransmitStatusPacketReceived(object sender, XbeeSerialPortControlPanel.TransmitStatusPacket packet)
        {

            textBox1.AppendText("TransmitStatusPacketReceived,FrameID:" + packet.frameID.ToString() + ",");
            switch (packet.status)
            {
                case XbeeSerialPortControlPanel.TXStatusPacketStatus.Success:
                    textBox1.AppendText("Success\r\n");
                    break;
                case XbeeSerialPortControlPanel.TXStatusPacketStatus.Purged:
                    textBox1.AppendText("Purged\r\n");
                    break;
                case XbeeSerialPortControlPanel.TXStatusPacketStatus.NoACKReceived:
                    textBox1.AppendText("NoACKReceived\r\n");
                    break;
                case XbeeSerialPortControlPanel.TXStatusPacketStatus.CCAfailure:
                    textBox1.AppendText("CCAfailure\r\n");
                    break;
            }
        }

        private void sendATCommandButton_Click(object sender, EventArgs e)
        {
            try
            {
                int frameID;
                byte[] sendData = new byte[0];
                if (atCommandParameter.Text != "")//パラメータ付きで送信するか
                {
                    sendData = TextToByteArray(atCommandParameter.Text);
                }
                frameID = xbeeSerialPortControlPanel1.sendAtCommand(
                    xbeeSerialPortControlPanel1.CreateATCommandData(
                    comboBox1.Text[0],
                    comboBox1.Text[1],
                    sendData
                    ));
                textBox1.AppendText(BitConverter.ToString(sendData));
                textBox1.AppendText("ATCommandRequest,FrameID:" + frameID.ToString() + "\r\n");
            }
            catch
            {
                textBox1.AppendText("送信処理中にエラーが発生しました。\r\n");
            }

        }

        private void send16BitButton_Click(object sender, EventArgs e)
        {
            try
            {
                byte[] sendData;
                sendData = TextToByteArray(sendDataTextBox.Text);
                string temp;
                temp = addressTextBox.Text.Replace("0x", "");

                if (sendData.Length != 0)
                {
                    textBox1.AppendText("16BitTransmitRequest,FrameID:" + xbeeSerialPortControlPanel1.send16BitAddressTxPacket(UInt16.Parse(temp, System.Globalization.NumberStyles.HexNumber), sendData).ToString() + "\r\n");
                }
            }
            catch
            {
                textBox1.AppendText("送信処理中にエラーが発生しました。\r\n");
            }
        }
        private byte[] TextToByteArray(string str){

            string[] splitData;
            if (str == "")
            {
                return new byte[0];
            }
            splitData = str.Split('-', ',');
            byte[] Data = new byte[splitData.Length];
            int i;
            for (i = 0; i < splitData.Length; i++)
            {
                if (splitData[i].Contains("0x"))
                {
                    splitData[i] = splitData[i].Replace("0x", "");
                    Data[i] = byte.Parse(splitData[i], System.Globalization.NumberStyles.HexNumber);
                }
                else
                {
                    Data[i] = byte.Parse(splitData[i]);
                }
            }
            return Data;
        }

        private void send64BitButton_Click(object sender, EventArgs e)
        {
            try
            {
                byte[] sendData;
                sendData = TextToByteArray(sendDataTextBox2.Text);
                string temp;
                temp = addressTextBox2.Text.Replace("0x", "");

                if (sendData.Length != 0)
                {
                    textBox1.AppendText("64BitTransmitRequest,FrameID:" + xbeeSerialPortControlPanel1.send64BitAddressTxPacket(UInt64.Parse(temp, System.Globalization.NumberStyles.HexNumber), sendData).ToString() + "\r\n");
                }
            }
            catch
            {
                textBox1.AppendText("送信処理中にエラーが発生しました。\r\n");
            }
        }

        private void logClearButton_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }
    }
}
